package com.xxwy.dependency.lookup;

import com.xxwy.dependency.User;
import org.springframework.beans.factory.ObjectProvider;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.Bean;

/**
 * @author wy
 * @date 2020/4/22 14:01
 */
public class ObjectProviderDemo {

  public static void main(String[] args) {
    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext();
    applicationContext.register(ObjectProviderDemo.class);
    applicationContext.refresh();

    //延迟查找
    lookupByObjectProvider(applicationContext);

    objectProviderStream(applicationContext);

    applicationContext.close();


  }

  @Bean
  public String hell() {
    return "hell";
  }

  @Bean
  public String he() {
    return "he";
  }


  private static void lookupByObjectProvider(AnnotationConfigApplicationContext context) {
    ObjectProvider<User> provider = context.getBeanProvider(User.class);
    System.out.println("hi");
    //User object = provider.getObject();
    User object = provider.getIfAvailable(User::new);
    System.out.println(object);
  }

  private static void objectProviderStream(AnnotationConfigApplicationContext context) {
    ObjectProvider<String> provider = context.getBeanProvider(String.class);
    provider.stream().forEach(System.out::println);
  }


}
